package com.heeexy.example.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.heeexy.example.dao.DeviceDao;
import com.heeexy.example.service.DeviceService;
import com.heeexy.example.util.CommonUtil;
import com.heeexy.example.util.constants.ErrorEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class DeviceServicelmpl implements DeviceService{
    @Autowired
    private DeviceDao deviceDao;


    /**
     * 新增设备
     */
    @Override
    public JSONObject addDevice(JSONObject jsonObject) {
        int exist = deviceDao.queryExistDeviceId(jsonObject);
        if (exist > 0) {
            return CommonUtil.errorJson(ErrorEnum.E_10009);
        }
        deviceDao.addDevice(jsonObject);
        return CommonUtil.successJson();
    }

    /**
     * 设备列表
     */
    @Override
    public JSONObject listDevice(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        int count = deviceDao.countDevice(jsonObject);
        List<JSONObject> list = deviceDao.listDevice(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    /*
     * 设备使用情况列表
     */
    @Override
    public JSONObject listUseTime(JSONObject jsonObject){
        CommonUtil.fillPageParam(jsonObject);
        int count = deviceDao.countDevice(jsonObject);
        List<JSONObject> list = deviceDao.listUseTime(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    /**
     * 更新设备
     */
    @Override
    public JSONObject updateDevice(JSONObject jsonObject) {
        deviceDao.updateDevice(jsonObject);
        return CommonUtil.successJson();
    }
    /**
     * 删除设备
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject deleteDevice(JSONObject jsonObject){
        deviceDao.deleteDevice(jsonObject);
        return CommonUtil.successJson();
    }

    @Override
    public JSONObject applyDevice(JSONObject jsonObject) {
        deviceDao.applyDevice(jsonObject);
        return CommonUtil.successJson();
    }

    @Override
    public JSONObject listApply(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        int count = deviceDao.countApply(jsonObject);
        List<JSONObject> list = deviceDao.listApply(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    @Override
    public JSONObject updateApply(JSONObject jsonObject) {
        deviceDao.updateApply(jsonObject);
        return CommonUtil.successJson();
    }

    @Override
    public JSONObject listRepair(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        int count = deviceDao.countRepair(jsonObject);
        List<JSONObject> list = deviceDao.listRepair(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    @Override
    public JSONObject updateRepair(JSONObject jsonObject) {
        deviceDao.updateRepair(jsonObject);
        return CommonUtil.successJson();
    }

    @Override
    public JSONObject searchDevice(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        int count = deviceDao.countSearchDevice(jsonObject);
        List<JSONObject> list = deviceDao.searchDevice(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    @Override
    public JSONObject listApplyUser(JSONObject jsonObject) {
        CommonUtil.fillPageParam(jsonObject);
        int count = deviceDao.countApplyUser(jsonObject);
        List<JSONObject> list = deviceDao.listApplyUser(jsonObject);
        return CommonUtil.successPage(jsonObject, list, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject deleteApply(JSONObject jsonObject){
        deviceDao.deleteApply(jsonObject);
        return CommonUtil.successJson();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject getDevicePosition(JSONObject jsonObject){
        List<JSONObject> list = deviceDao.getDevicePosition(jsonObject);
        return CommonUtil.successPage(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject getDeviceName(JSONObject jsonObject){
        List<JSONObject> list = deviceDao.getDeviceName(jsonObject);
        return CommonUtil.successPage(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject getDeviceId(JSONObject jsonObject){
        List<JSONObject> list = deviceDao.getDeviceId(jsonObject);
        return CommonUtil.successPage(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject getDeviceDetail(JSONObject jsonObject){
        List<JSONObject> list = deviceDao.getDeviceDetail(jsonObject);
        return CommonUtil.successPage(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject getDeviceFunction(JSONObject jsonObject){
        List<JSONObject> list = deviceDao.getDeviceFunction(jsonObject);
        return CommonUtil.successPage(list);
    }

}
